package jogo;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class GameMidlet extends MIDlet implements CommandListener {

    private Jogo gameCanvas;
    private Thread t;
    private Display d;
    private Form formMaim = null;
    private Ticker ticker = null;
    private Image image = null;
    private Command commExit, commNovoJogo,
            commInstrucoes, commLogin, commHelp, commNivelFacil, commNivelDificil;
    private Alert instrucao;
    private Alert ajuda;
    private Form formNivel = null;

    public void startApp() {

        d = Display.getDisplay(this);
        formMaim = new Form("Bode Runner");
        formMaim.addCommand(commExit = new Command("Sair", Command.EXIT, 0));
        formMaim.addCommand(commNovoJogo = new Command("Novo Jogo", Command.OK, 1));
        formMaim.addCommand(commInstrucoes = new Command("Instruções", Command.OK, 1));
        formMaim.addCommand(commHelp = new Command("Ajuda", Command.HELP, 1));
        formMaim.setTicker(ticker = new Ticker("Guilherme, Hugo, Marcio, Petrônio e Thiago"));

        try {
            image = Image.createImage("/bode2.png");

        } catch (Exception e) {
            e.printStackTrace();
        }
        formMaim.append(new ImageItem(null, image, ImageItem.LAYOUT_CENTER, null));
        formMaim.setCommandListener(this);

        instrucao = new Alert("Instruções", "Utilize as setas direcionais para movimentar o bode pelo cenário. " +
                "Busque a comida que está em latas para ganhar mais tempo, " +
                "encontre o caminho de fuga na fazenda e o IBAMA na cidade.\nTudo isso sem ser visto pelos Materazzi!!", null, null);
        //instrucao.setTimeout(Alert.FOREVER);

        ajuda = new Alert("Ajuda", "Colocar o texto ajuda aqui!!!", null, null);
        ajuda.setTimeout(Alert.FOREVER);


        d.setCurrent(formMaim);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
        this.gameCanvas.getFaseAtual().stop();
    }

    public void commandAction(Command c, Displayable d) {
        if (c == commExit) {
            destroyApp(false);
            notifyDestroyed();
        } else if (c == commNovoJogo) {
            formNivel = new Form("Escolha o Nível do Jogo");
            commNivelFacil = new Command("Nível Fácil", Command.OK, 1);
            commNivelDificil = new Command("Nível Difícil", Command.OK, 1);
            formNivel.append(new ImageItem(null, image, ImageItem.LAYOUT_CENTER, null));
            formNivel.addCommand(commNivelFacil);
            formNivel.addCommand(commNivelDificil);
            formNivel.setCommandListener(this);
            Display.getDisplay(this).setCurrent(formNivel);

        } else if (c == commInstrucoes) {
            Display.getDisplay(this).setCurrent(instrucao);

        } else if (c == commHelp) {
            Display.getDisplay(this).setCurrent(ajuda);
        } else if (c == commNivelFacil) {
            this.gameCanvas = new Jogo(this, Jogo.FACIL);
            this.t = new Thread(gameCanvas);
            t.start();
            Display.getDisplay(this).setCurrent(gameCanvas.getFaseAtual());
        }else if(c == commNivelDificil){
               this.gameCanvas = new Jogo(this, Jogo.DIFICIL);
            this.t = new Thread(gameCanvas);
            t.start();
            Display.getDisplay(this).setCurrent(gameCanvas.getFaseAtual());
        }

    }

    public Alert getAjuda() {
        return ajuda;
    }

    public void setAjuda(Alert ajuda) {
        this.ajuda = ajuda;
    }

    public Command getCommExit() {
        return commExit;
    }

    public void setCommExit(Command commExit) {
        this.commExit = commExit;
    }

    public Command getCommHelp() {
        return commHelp;
    }

    public void setCommHelp(Command commHelp) {
        this.commHelp = commHelp;
    }

    public Command getCommInstrucoes() {
        return commInstrucoes;
    }

    public void setCommInstrucoes(Command commInstrucoes) {
        this.commInstrucoes = commInstrucoes;
    }

    public Command getCommLogin() {
        return commLogin;
    }

    public void setCommLogin(Command commLogin) {
        this.commLogin = commLogin;
    }

    public Command getCommNovoJogo() {
        return commNovoJogo;
    }

    public void setCommNovoJogo(Command commNovoJogo) {
        this.commNovoJogo = commNovoJogo;
    }

    public Display getD() {
        return d;
    }

    public void setD(Display d) {
        this.d = d;
    }

    public Form getFormMaim() {
        return formMaim;
    }

    public void setFormMaim(Form formMaim) {
        this.formMaim = formMaim;
    }

    public Jogo getGameCanvas() {
        return gameCanvas;
    }

    public void setGameCanvas(Jogo gameCanvas) {
        this.gameCanvas = gameCanvas;
    }

    public Image getImage() {
        return image;
    }

    public void setImage(Image image) {
        this.image = image;
    }

    public Alert getInstrucao() {
        return instrucao;
    }

    public void setInstrucao(Alert instrucao) {
        this.instrucao = instrucao;
    }

    public Thread getT() {
        return t;
    }

    public void setT(Thread t) {
        this.t = t;
    }

    public Ticker getTicker() {
        return ticker;
    }

    public void setTicker(Ticker ticker) {
        this.ticker = ticker;
    }
}
